package com.studentmanage.manage_course.service;

import com.studentmanage.framework.domain.base.Course;
import com.studentmanage.framework.domain.base.request.QueryCourse;
import com.studentmanage.framework.domain.common.response.CommonCode;
import com.studentmanage.framework.domain.common.response.QueryResult;
import com.studentmanage.framework.domain.common.response.ResponseResult;
import com.studentmanage.framework.domain.common.response.ResponseValue;
import com.studentmanage.framework.utils.PageSizeUtil;
import com.studentmanage.manage_course.dao.CourseMapper;
import com.studentmanage.manage_course.dao.CourseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseService {
    @Autowired
    private CourseRepository courseRepository;
    @Autowired(required = false)
    private CourseMapper courseMapper;

    //增加课程
    public ResponseResult addCourse(Course course) {
        if(course == null){
            return new ResponseResult(CommonCode.FAIL);
        }
        courseRepository.save(course);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    public QueryResult<Course> getList() {
        List<Course> list = courseRepository.findAll();
        QueryResult<Course> queryResult = new QueryResult<>();
        queryResult.setList(list);
        return queryResult;
    }

    //分页查询课程列表
    public QueryResult<Course> findCourseList(int page, int size, QueryCourse queryCourse) {
        queryCourse.setPage(PageSizeUtil.formatPage(page,size));
        queryCourse.setSize(PageSizeUtil.formatSize(size));
        List<Course> list = courseMapper.findAllLikeName(queryCourse);
        Integer total = courseMapper.getTotal(queryCourse);
        QueryResult<Course> queryResult = new QueryResult<>();
        queryResult.setList(list);
        queryResult.setTotal(total);
        return queryResult;
    }
}
